package com.wyt.controller.instorageController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyt.dao.InstOrAgeAndDetailMapper;
import com.wyt.dao.InstOrAgeMapper;
import com.wyt.dao.PurchaseDetailMapper1;
import com.wyt.dao.PurchaseMapper1;
import com.wyt.entity.*;
import com.wyt.until.CommonResult;
import com.wyt.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class InsertController {
    @Autowired
    private InstOrAgeMapper instOrAgeMapper;
    @Autowired
    private InstOrAgeAndDetailMapper instOrAgeAndDetailMapper;
    @Autowired
    private PurchaseMapper1 purchaseMapper1;
    @Autowired
    private PurchaseDetailMapper1 purchaseDetailMapper1;
    @RequestMapping("insertruku")
    @ResponseBody
      public Result insertruku(String data, HttpServletRequest request) throws JsonProcessingException {
        Employee login = (Employee) request.getSession().getAttribute("login");
        String name = login.geteName();
        Integer integer = login.geteNumber();
        //生成日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = new Date();
        simpleDateFormat.format(date1);
        Purchase1 purchase1 = new ObjectMapper().readValue(data,Purchase1.class);
        purchase1.getPid();
        InstOrAge instOrAge = new InstOrAge();
        instOrAge.setPname(name);
        instOrAge.setUid(purchase1.getUid());
        instOrAge.setTotalCount(purchase1.getTotalCount());
        instOrAge.setTotalPrice(purchase1.getTotalPrice());
        instOrAge.setPdate(date1);
        instOrAge.setStatus(3);
        instOrAgeMapper.insetOutstorAge(instOrAge);
        purchaseMapper1.updateStatus(purchase1.getPid());

        List<PurchaseDetail1> list = purchaseDetailMapper1.findAllByPid(purchase1.getPid());
        System.out.println("==========================================================================");
        System.out.println(list);
        InstOrAgeAndDetail instOrAgeAndDetail = new InstOrAgeAndDetail();
        for (PurchaseDetail1 purchaseDetail1:list){
          instOrAgeAndDetail.setBookid(purchaseDetail1.getBookid());
          instOrAgeAndDetail.setBookname(purchaseDetail1.getBookname());
          instOrAgeAndDetail.setAuthor(purchaseDetail1.getAuthor());
          instOrAgeAndDetail.setPublisher(purchaseDetail1.getPublisher());
          instOrAgeAndDetail.setCount(purchaseDetail1.getCount());
          instOrAgeAndDetail.setPrice(purchaseDetail1.getPrice());
          instOrAgeAndDetail.setBooktype(purchaseDetail1.getBooktype());
          instOrAgeAndDetail.setPid(instOrAge.getPid());
         instOrAgeAndDetailMapper.insert(instOrAgeAndDetail);
        }

//        instOrAgeAndDetail.getBookid();
       return new Result("1","");
    }

}
